﻿package com.peachcastle.core
{
	import com.greensock.easing.Back;
	import com.greensock.easing.Linear;
	import com.greensock.TweenLite;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import com.peachcastle.core.Display;

	public class TimerCircle extends Display
	{
		private var _timerMask:Shape = null;
		private var _timerRotation:MovieClip = null;
		private var _timer:Timer = null;
		private var _time:int = 30; //时间总量 默认时间数
		private var _curtime:int = 0; //当前时间
		private var _isPlayTimeSound:Boolean = false; // 是否播放定时音
		private var _tween:TweenLite = null;
		private var _tweenShow:TweenLite = null;
		private var _rate:int = 1;
		
		
		/**
		 * 构造函数
		 * @param	obj
		 */
		public function TimerCircle(ui:MovieClip = null):void 
		{
			super(ui);
			
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			assest.cacheAsBitmap = true;
			_timerMask = new Shape();
			_timerRotation = new MovieClip();
			assest.line.mask = _timerMask;
			assest.addChild(_timerMask);
			
			var delay:int = 1000 / _rate;
			_time *= _rate;
			_curtime = _time-_rate;
			_timer = new Timer(delay, _time);
		}
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
			_timer.addEventListener(TimerEvent.TIMER, OnTimer);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, OnTimerComplete); 
		}
		
		
		
		//定时器事件
		public function OnTimer(e:TimerEvent):void
		{
			_curtime--;
			
			dispatchEvent(new Event(Event.CHANGE));
		}
	
		
		/**
		 * 开始
		 */
		public function Start(chiptime:int, max:int = 30):void 
		{
			assest.visible = true;
			_timer.reset();
			_timer.repeatCount = chiptime * _rate;
			_timer.start();
			_time = chiptime * _rate;
			_curtime = chiptime * _rate;
			_isPlayTimeSound = false;
			
			_timerRotation.rotation = (360 / max) * (max - chiptime);
			
			var rot:int = (max / 2 <= chiptime)?360:0;
			
			DrawSector(_timerMask, 0, 0, 100, -360, 270);
			
			_tween = TweenLite.to(_timerRotation, chiptime, { rotation:rot, 
															  onUpdate:onUpdataFunction,  
															  onComplete:onCompleteFunction,  
															  ease:Linear.easeNone } );
			
															  
			tweenShow();
		}
		
		private function tweenShow():void
		{
			assest.scaleX = assest.scaleY = 0.8;
			assest.alpha = 0;
			_tweenShow = TweenLite.to(assest, 0.4, {scaleX:1, scaleY:1, alpha:1, ease:Back.easeOut});
		}
		
		private function onUpdataFunction():void
		{
			_timerMask.graphics.clear();
			var rot:Number = _timerRotation.rotation > 0?_timerRotation.rotation:_timerRotation.rotation + 360;

			DrawSector(_timerMask, 0, 0, 100, rot-360);
		}
		
		private function onCompleteFunction():void
		{
			_timerRotation.rotation = -360;
		}
		
		/**
		 * 手动停止
		 */
		public function Stop():void 
		{
			assest.visible = false;
			_timer.reset();
			_timerRotation.rotation = 0;
			if (_tween)_tween.kill();//TweenLite.removeTween(_tween);
			if (_tweenShow)_tweenShow.kill();//TweenLite.removeTween(_tweenShow);
			assest.scaleX = assest.scaleY = 1;
			assest.alpha = 1;
		}
		
		/**
		 * 自动停止
		 * @param	e
		 */
		public function OnTimerComplete(e:TimerEvent):void
		{
			assest.visible = false;
			_timerRotation.rotation = 0;
			if (_tween) _tween.kill();//TweenLite.removeTween(_tween);
			if (_tweenShow)_tweenShow.kill();
				//TweenLite.removeTween(_tweenShow);
			assest.scaleX = assest.scaleY = 1;
			assest.alpha = 1;
		}
		
		public function DrawSector(mc:Shape, x:Number = 0, y:Number = 0, r:Number = 0, angle:Number = 0, startFrom:Number = 270, color:Number = 0xff0000):void 
		{
			
			mc.graphics.beginFill(color,50);
			mc.graphics.moveTo(x,y);
			var n:Number = Math.ceil(Math.abs(angle) / 45);
			var angleA:Number = angle / n;
			angleA = angleA * Math.PI / 180;
			startFrom = startFrom * Math.PI / 180;
			mc.graphics.lineTo(x, y - r);
			for (var i:int = 1; i <= n; i++) 
			{
				startFrom += angleA;
				var angleMid:Number = startFrom - angleA / 2;
				var bx:Number = x + r / Math.cos(angleA / 2) * Math.cos(angleMid);
				var by:Number = y + r / Math.cos(angleA / 2) * Math.sin(angleMid);
				var cx:Number = x + r * Math.cos(startFrom);
				var cy:Number = y + r * Math.sin(startFrom);
				mc.graphics.curveTo(bx, by, cx, cy);
			}
			mc.graphics.endFill();
		}
		
		public function get time():int { return _curtime; }
	}
	
}